package cz.studio.baseDao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public abstract class StudioHibernateSession {
	
	private static Configuration conf;

	public static Session getSession() {
		initConf();
		SessionFactory sessionFactory = null;
		try {
			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
					.applySettings(conf.getProperties()).buildServiceRegistry();
			sessionFactory = conf.buildSessionFactory(serviceRegistry);
			return sessionFactory.openSession();
		} catch (Throwable ex) {
			System.err.println("Failed to create sessionFactory object." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}
	
	private static void initConf() {
		conf = new Configuration();
		conf.addResource("uzivatel.hbm.xml");
		conf.configure();
	}

}
